package com.mer.controller;

import com.mer.entity.Subject;
import com.mer.service.SubjectService;
import com.mer.vo.CommonResult;
import com.mer.vo.SubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @program: IIPhase-project
 * @description: 课程分类控制层
 * @author: 陈铠
 * @create: 2021-10-19 10:41
 **/
@RestController
@RequestMapping("/core/subject")
@Api("查询课程分类的api接口")
public class SubjectController {
    @Resource
    private SubjectService subjectService;

    @ApiOperation("分页查询课程分类")
    @PostMapping("/findSubject/{page}/{limit}")
    public CommonResult selectSubjectByPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @RequestBody SubjectVo subjectVo) {
        CommonResult commonResult = subjectService.selectSubjectByPage(page, limit, subjectVo);
        return commonResult;
    }

    @ApiOperation("添加课程分类、子课程")
    @PostMapping("/insertSubject")
    public CommonResult insertSubject(@RequestBody Subject subject) {
        String s = UUID.randomUUID().toString().replace("-", "").substring(0, 19);
        if ("0".equals(subject.getParentId()) && subject.getId().equals("0")) {
            subject.setSort(0);
            subject.setId(s);
        } else {
            subject.setParentId(subject.getId());
            subject.setId(s);
            subject.setSort(1);
        }
        subject.setStatusId("0");
        LocalDateTime now = LocalDateTime.now();
        subject.setGmtCreate(now.toString());
        subject.setGmtModified(now.toString());
        return subjectService.insertSubject(subject);
    }

    @ApiOperation("删除课程分类、子课程")
    @PostMapping("/deleteSubject")
    public CommonResult deleteSubject(@RequestBody Subject subject) {
        String id = subject.getId();
        return subjectService.deleteSubject(id);
    }

    @ApiOperation("修改课程分类、子课程")
    @PostMapping("/updateSubject")
    public CommonResult updateSubject(@RequestBody Subject subject) {
        return subjectService.updateSubject(subject);
    }

    @ApiOperation("修改课程状态")
    @PostMapping("/changeStatus")
    public CommonResult changeStatus(@RequestBody SubjectVo subjectVo) {
        return subjectService.changeStatus(subjectVo);
    }

    @ApiOperation("查找正常的上架课程，用来填充下拉框")
    @PostMapping("/selectSubjectByStatus/{page}/{limit}")
    public CommonResult selectSubjectByStatus(@PathVariable("page") Integer page, @PathVariable Integer limit) {
        return subjectService.selectSubjectByStatus(page, limit);
    }

    @ApiOperation("根据课程分类id查询父id")
    @PostMapping("/subjectPidById/{pid}")
    public String subjectPidById(@PathVariable String pid) {
        return subjectService.subjectPidById(pid);
    }

}
